package bb.lanxing.lib.devices.sprint.utils;

import bb.lanxing.activity.deeplink.DeepLinkPathConstants;
import bb.lanxing.lib.base.BaseApplication;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SprintSyncLogFileUtils {
    public static final String SYNC_ERRORLOG_DIRPATH = "sync/error/";
    public static final String SYNC_ERRORLOG_FILENAME = "sync_error.txt";
    public static final String SYNC_LOG_DIRPATH = "sync/log/";
    public static final String SYNC_LOG_FILENAME = "sync_log.txt";

    public static String buildExternalDirectoryPath(String... strArr) {
        StringBuilder sb = new StringBuilder();
        sb.append(BaseApplication.get().getExternalFilesDir(null));
        sb.append(File.separatorChar);
        sb.append(DeepLinkPathConstants.SCHEME_APP_LANXING);
        for (String str : strArr) {
            sb.append(File.separatorChar);
            sb.append(str);
        }
        String sb2 = sb.toString();
        if (!ensureDirectoryExists(sb2)) {
            String builderMntDirectoryPath = builderMntDirectoryPath(strArr);
            if (!ensureDirectoryExists(builderMntDirectoryPath)) {
                return null;
            }
            return builderMntDirectoryPath;
        }
        return sb2;
    }

    private static String builderMntDirectoryPath(String... strArr) {
        StringBuilder sb = new StringBuilder();
        sb.append(File.separatorChar);
        sb.append("mnt");
        sb.append(File.separatorChar);
        sb.append("sdcard");
        sb.append(File.separatorChar);
        sb.append(DeepLinkPathConstants.SCHEME_APP_LANXING);
        for (String str : strArr) {
            sb.append(File.separatorChar);
            sb.append(str);
        }
        return sb.toString();
    }

    public static boolean ensureDirectoryExists(String str) {
        return ensureDirectoryExists(new File(str));
    }

    private static boolean ensureDirectoryExists(File file) {
        return (file.exists() && file.isDirectory()) || file.mkdirs();
    }

    public static void saveSyncELog(String str) {
        StringBuilder sb = new StringBuilder();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        sb.append(format + " : ");
        sb.append(str);
        sb.append("\n");
        String sb2 = sb.toString();
        saveStringToFile(sb2, buildExternalDirectoryPath(SYNC_ERRORLOG_DIRPATH) + SYNC_ERRORLOG_FILENAME);
    }

    public static void saveSyncLog(String str) {
        StringBuilder sb = new StringBuilder();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        sb.append(format + " : ");
        sb.append(str);
        sb.append("\n");
        String sb2 = sb.toString();
        saveStringToFile(sb2, buildExternalDirectoryPath(SYNC_LOG_DIRPATH) + SYNC_LOG_FILENAME);
    }

    public static void saveStringToFile(String str, String str2) {
        try {
            FileWriter fileWriter = new FileWriter(new File(str2), true);
            fileWriter.write(str);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
